package com.gettec.lims.server.services;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;

import org.apache.catalina.User;





import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.store.ListStore;
import com.gettec.lims.client.services.NnimsService;
import com.gettec.lims.server.util.Mysql;
import com.gettec.lims.shared.model.RegSheet;
import com.gettec.lims.shared.model.SampleInfo;
import com.gettec.lims.shared.model.SysUser;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class NnimsServiceImpl extends RemoteServiceServlet implements
NnimsService {

	@Override
	public void createNewFeed() {
		 UUID uuid = UUID.randomUUID();  
	       // return new Feed(uuid.toString());  

	}
	
	public void saveFeed(Object feed) {}

	@Override
	public RegSheet showRegSheetFormPage() {
		return new RegSheet();///.init(6, "数据范围", "entrust_org", Date.valueOf("2012-12-12") ,Date.valueOf("2012-06-12"), "under_dept", "bussiness_type", "area_info", "detect_type", "detect_way", "report_send_way", true, false, "pay_type", "entrust_reg", "service_declear", "accepter", "entruster");
	}

	@Override
	public List<SysUser> getUserList() {
		List<SysUser> l=Mysql.query(SysUser.class, "select * from sys_user");
		return l; 

	}

	@Override
	public PagingLoadResult<SampleInfo> getSampleInfos(PagingLoadConfig config) {
		// TODO Auto-generated method stub
		
			   List<SampleInfo> sampleInfos=loadSampleInfoList();
			    if (config.getSortInfo().getSortField() != null) {
			      final String sortField = config.getSortInfo().getSortField();
			      if (sortField != null) {
			        Collections.sort(sampleInfos, config.getSortInfo().getSortDir().comparator(new Comparator() {
			          public int compare(Object o1, Object o2) {
			        	  SampleInfo s1 = (SampleInfo) o1;
			        	  SampleInfo s2 = (SampleInfo) o2;
			            if (sortField.equals("production_date")) {
			              return s1.getProduction_date().compareTo(s2.getProduction_date());
			            } else if (sortField.equals("level")) {
			              return s1.getLevel().compareTo(s2.getLevel());
			            } else if (sortField.equals("price")) {
			              return s1.getPrice().compareTo(s2.getPrice());
			            } else if (sortField.equals("inventory_volume")) {
			              return s1.getInventory_volume().compareTo(s2.getInventory_volume());
			            }
			            return 0;
			          }
			        }));
			      }
			    }

			    ArrayList<SampleInfo> sublist = new ArrayList<SampleInfo>();
			    int start = config.getOffset();
			    int limit = sampleInfos.size();
			    if (config.getLimit() > 0) {
			      limit = Math.min(start + config.getLimit(), limit);
			    }
			    for (int i = config.getOffset(); i < limit; i++) {
			      sublist.add(sampleInfos.get(i));
			    }
			    return new BasePagingLoadResult<SampleInfo>(sublist, config.getOffset(), sampleInfos.size());
		
	}  
public List<SampleInfo> loadSampleInfoList()
{
	return Mysql.query(SampleInfo.class, "select * from sampleinfo");
}

@Override
public PagingLoadResult<RegSheet> getRegSheets(PagingLoadConfig config) {
	 List<RegSheet> sampleInfos= Mysql.query(RegSheet.class, "select * from regsheet");
	ArrayList<RegSheet> sublist = new ArrayList<RegSheet>();
    int start = config.getOffset();
    int limit = sampleInfos.size();
    if (config.getLimit() > 0) {
      limit = Math.min(start + config.getLimit(), limit);
    }
    for (int i = config.getOffset(); i < limit; i++) {
      sublist.add(sampleInfos.get(i));
    }
    return new BasePagingLoadResult<RegSheet>(sublist, config.getOffset(), sampleInfos.size());
	
}

@Override
public int saveRegSheet(RegSheet regSheet) {
	return Mysql.save(regSheet);
}

@Override
public int deleteRegSheet(RegSheet regSheet) {
	return Mysql.delete(regSheet);
}

@Override
public int updateRegSheet(RegSheet regSheet) {
	return Mysql.update(regSheet);
}

@Override
public List<RegSheet> getClientSerialList() {
	// TODO Auto-generated method stub
	return Mysql.query(RegSheet.class, "select * from regsheet");
}

@Override
public int saveSampleInfo(SampleInfo sampleInfo) {
	// TODO Auto-generated method stub
	return Mysql.save(sampleInfo);
}
}
